package com.alittle.support.support.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.ViewFlipper;

/**
 * 多加载状态的 ViewFlipper
 * <p/>
 * 感觉这个东西设计有点问题，最好别用 —— 肖平原
 * <p/>
 * 默认顺序：
 * 1. STATE_LOADING
 * 2. STATE_LOAD_ERROR
 * 3. STATE_LOAD_SUCCESS_EMPTY
 * 4. STATE_LOAD_SUCCESS_CONTENT
 */
public class MultiStateFlipper<D, E> extends ViewFlipper implements MultiStateView<D, E> {
    public MultiStateFlipper(Context context) {
        super(context);
    }

    public MultiStateFlipper(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public void showMultiStateLoading() {
        setDisplayedChild(0);
    }

    @Override
    public void showMultiStateLoadError(E error) {
        setDisplayedChild(1);
    }

    @Override
    public void showMultiStateLoadSuccessEmpty() {
        setDisplayedChild(2);
    }

    @Override
    public void showMultiStateLoadSuccessContent(D content) {
        setDisplayedChild(3);
    }

    /**
     * 返回当前的 MultiState 状态
     */
    public int getCurrentMultiState() {
        int displayedChild = getDisplayedChild();
        switch (displayedChild) {
            case 0:
                return STATE_LOADING;
            case 1:
                return STATE_LOAD_ERROR;
            case 2:
                return STATE_LOAD_SUCCESS_EMPTY;
            case 3:
                return STATE_LOAD_SUCCESS_CONTENT;
            default:
                return STATE_UNKNOWN;
        }
    }
}
